using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Runtime.Serialization;

namespace WcfService1.DTO
{
    [DataContract]
    public class PurchaseInvoiceDTO : INotifyPropertyChanged
    {
        private Decimal _AfterDiscount;
        private Decimal _BiayaForwarder;
        //private Decimal _BiayaLain;
        //private Decimal _BiayaTrucking;
        private ObservableCollection<BTBDTO> _Btbs;
        private CurrencyDTO _Currency;
        private String _Description;
        private Decimal _Discount;
        private String _DiscountType;
        private bool _IsPpn;
        private Decimal _Kurs;
        private int _No;
        private DateTime? _PIDate;
        private String _PINo;
        private Decimal _Ppn;
        private ObservableCollection<PurchaseInvoiceDetailDTO> _PurchaseInvoiceDetails;
        private Status _Status;
        private Decimal _SubTotal;
        private SupplierDTO _Supplier;
        private String _Term;
        private Decimal _Total;
        private String _created_by;
        private DateTime? _created_date;
        private String _modified_by;
        private DateTime? _modified_date;
        private string _NoPO;


        public PurchaseInvoiceDTO()
        {
            PIDate = DateTime.Now.Date;
            Btbs = new ObservableCollection<BTBDTO>();
        }

        [DataMember]
        public String PINo
        {
            get { return _PINo; }
            set
            {
                _PINo = value;
                RaisePropertyChanged("PINo");
            }
        }


        [DataMember]
        public DateTime? PIDate
        {
            get { return _PIDate; }
            set
            {
                _PIDate = value;
                RaisePropertyChanged("PIDate");
            }
        }


        [DataMember]
        public ObservableCollection<BTBDTO> Btbs
        {
            get { return _Btbs; }
            set
            {
                _Btbs = value;
                RaisePropertyChanged("Btbs");
            }
        }


        [DataMember]
        public SupplierDTO Supplier
        {
            get { return _Supplier; }
            set
            {
                _Supplier = value;
                RaisePropertyChanged("Supplier");
            }
        }


        [DataMember]
        public CurrencyDTO Currency
        {
            get { return _Currency; }
            set
            {
                _Currency = value;
                RaisePropertyChanged("Currency");
            }
        }


        [DataMember]
        public Decimal Kurs
        {
            get { return _Kurs; }
            set
            {
                _Kurs = value;
                RaisePropertyChanged("Kurs");
            }
        }


        [DataMember]
        public String Term
        {
            get { return _Term; }
            set
            {
                _Term = value;
                RaisePropertyChanged("Term");
            }
        }


        [DataMember]
        public Decimal Ppn
        {
            get { return _Ppn; }
            set
            {
                _Ppn = value;
                RaisePropertyChanged("Ppn");
            }
        }


        [DataMember]
        public String Description
        {
            get { return _Description; }
            set
            {
                _Description = value;
                RaisePropertyChanged("Description");
            }
        }


        [DataMember]
        public Decimal BiayaForwarder
        {
            get { return _BiayaForwarder; }
            set
            {
                _BiayaForwarder = value;
                RaisePropertyChanged("BiayaForwarder");
            }
        }


        //[DataMember]
        //public Decimal BiayaTrucking
        //{
        //    get { return _BiayaTrucking; }
        //    set
        //    {
        //        _BiayaTrucking = value;
        //        RaisePropertyChanged("BiayaTrucking");
        //    }
        //}


        //[DataMember]
        //public Decimal BiayaLain
        //{
        //    get { return _BiayaLain; }
        //    set
        //    {
        //        _BiayaLain = value;
        //        RaisePropertyChanged("BiayaLain");
        //    }
        //}


        [DataMember]
        public String created_by
        {
            get { return _created_by; }
            set
            {
                _created_by = value;
                RaisePropertyChanged("created_by");
            }
        }


        [DataMember]
        public DateTime? created_date
        {
            get { return _created_date; }
            set
            {
                _created_date = value;
                RaisePropertyChanged("created_date");
            }
        }

        [DataMember]
        public String modified_by
        {
            get { return _modified_by; }
            set
            {
                _modified_by = value;
                RaisePropertyChanged("modified_by");
            }
        }


        [DataMember]
        public DateTime? modified_date
        {
            get { return _modified_date; }
            set
            {
                _modified_date = value;
                RaisePropertyChanged("modified_date");
            }
        }

        [DataMember]
        public ObservableCollection<PurchaseInvoiceDetailDTO> PurchaseInvoiceDetails
        {
            get { return _PurchaseInvoiceDetails; }
            set
            {
                if (value == PurchaseInvoiceDetails) return;
                _PurchaseInvoiceDetails = value;
                RaisePropertyChanged("PurchaseInvoiceDetails");
                RefreshNumber();
            }
        }


        [DataMember]
        public Decimal Discount
        {
            get { return _Discount; }
            set
            {
                _Discount = value;
                RaisePropertyChanged("Discount");
            }
        }


        [DataMember]
        public String DiscountType
        {
            get { return _DiscountType; }
            set
            {
                _DiscountType = value;
                RaisePropertyChanged("DiscountType");
            }
        }

        [DataMember]
        public Status Status
        {
            get { return _Status; }
            set
            {
                _Status = value;
                RaisePropertyChanged("Status");
            }
        }
       

        [DataMember]
        public string NoPO
        {
            get { return _NoPO; }
            set
            {
                _NoPO = value;
                RaisePropertyChanged("NoPO");
            }
        }

        public int No
        {
            get { return _No; }
            set
            {
                _No = value;
                RaisePropertyChanged("No");
            }
        }

        [DataMember]
        public Decimal Total
        {
            get { return _Total; }
            set
            {
                _Total = value;
                RaisePropertyChanged("Total");
            }
        }

        public bool IsPpn
        {
            get { return _IsPpn; }
            set
            {
                _IsPpn = value;
                RaisePropertyChanged("IsPpn");
            }
        }

        public Decimal SubTotal
        {
            get { return _SubTotal; }
            set
            {
                _SubTotal = value;
                RaisePropertyChanged("SubTotal");
            }
        }

        public Decimal AfterDiscount
        {
            get { return _AfterDiscount; }
            set
            {
                _AfterDiscount = value;
                RaisePropertyChanged("AfterDiscount");
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        private void RefreshNumber()
        {
            PurchaseInvoiceDetails.CollectionChanged += PurchaseInvoiceDetails_CollectionChanged;
            int number = 0;
            foreach (PurchaseInvoiceDetailDTO DetailDto in PurchaseInvoiceDetails)
            {
                number += 1;
                DetailDto.No = number;
            }
        }

        private void PurchaseInvoiceDetails_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            int number = 0;
            foreach (PurchaseInvoiceDetailDTO DetailDto in PurchaseInvoiceDetails)
            {
                number += 1;
                DetailDto.No = number;
            }
        }

        protected void RaisePropertyChanged(string propertyname)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyname));
        }

        public override string ToString()
        {
            return this.PINo;
        }
    }
}